from apps.common.base.dao import BaseDao
from apps.merchant.item.serializers import *
from django.db.models import F

class ItemDao(BaseDao):
    MODEL_CLASS = Item
    SERI_CLASS = ItemBaseSerializer

    # 增加库存
    def addStock(self, itemId, n):
        item = ItemDao().findOneByObjId(itemId)
        item.stock = F('stock') + n
        item.save()

    # 减少库存
    def subStock(self, itemId, n):
        item = ItemDao().findOneByObjId(itemId)
        if item.stock > n:
            item.stock = F('stock') - n
        else:
            item.stock = 0
        item.save()

    # 增加销量
    def addSales(self, itemId, n):
        item = ItemDao().findOneByObjId(itemId)
        item.sales = F('sales') + n
        item.save()

    # 减少销量
    def subSales(self, itemId, n):
        item = ItemDao().findOneByObjId(itemId)
        item.sales = F('sales') - n
        item.save()